package com.atguigu03.threadsafe.notsafe;


//继承的方式
class SaleTicket extends Thread {
    static int tickets = 100;

    @Override
    public void run() {
        while (true) {
            if(tickets > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + tickets);
                tickets--;
            } else {
                break;
            }

        }
    }
}

//实现接口的方式
class SlaeTicket2 implements Runnable {

    int tickets = 100;
    @Override
    public void run() {
        while (true) {
            if(tickets > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + tickets);
                tickets--;
            } else {
                break;
            }

        }
    }
}

public class WindowTest {
    public static void main(String[] args) {

//        SaleTicket t1 = new SaleTicket();
//        SaleTicket t2 = new SaleTicket();
//        SaleTicket t3 = new SaleTicket();
//
//        t1.start();
//        t2.start();
//        t3.start();

        SlaeTicket2 slaeTicket2 = new SlaeTicket2();
        Thread thread = new Thread(slaeTicket2);
        Thread thread2 = new Thread(slaeTicket2);
        Thread thread3 = new Thread(slaeTicket2);
        thread.start();
        thread2.start();
        thread3.start();

    }
}
